<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\ContactFollow\Action\Sidebar;

use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\SidebarAuthMiddleware;
use MoChat\App\WorkContact\Contract\WorkContactContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\ContactFollow\Contract\ContactFollowContract;
use MoChat\Plugin\ContactFollow\Contract\ContactFollowTemplateContract;
use MoChat\Plugin\ContactTrack\Contract\ContactTrackContract;

/**
 * 跟进详情.
 *
 * Class Info
 * @Controller
 */
class Info extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var WorkContactContract
     */
    protected $workContact;

    /**
     * @Inject
     * @var ContactFollowContract
     */
    protected $contactFollowService;

    /**
     * @Inject
     * @var StdoutLoggerInterface
     */
    protected $logger;

    /**
     * 互动轨迹表.
     * @Inject
     * @var ContactTrackContract
     */
    private $track;

    /**
     * @Inject
     * @var ContactFollowTemplateContract
     */
    private $contactFollowTemplateService;

    /**
     * @Middlewares({
     *     @Middleware(SidebarAuthMiddleware::class)
     * })
     * @RequestMapping(path="/sidebar/contactFollow/info", methods="GET")
     */
    public function handle(): array
    {
        // 接收参数
        $params['targetId'] = (int) $this->request->input('targetId');
        // 校验参数
        $this->validated($params);

        # # 跟进
        $info = $this->contactFollowService->getContactFollowById($params['targetId'], ['follow_template_id', 'content']);
        $info['processTemplateName'] = '';
        if ($info['followTemplateId'] > 0) {
            $info['processTemplateName'] = $this->contactFollowTemplateService->getContactFollowTemplateById($info['followTemplateId'], ['name'])['name'];
        }
        return $info;
    }

    /**
     * @return string[] 规则
     */
    public function rules(): array
    {
        return [
            'targetId' => 'required|integer|min:1|bail',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    public function messages(): array
    {
        return [
            'targetId.required' => '跟进id必传',
        ];
    }
}
